var express = require('express');
var router = express.Router();
let jwt = require('jsonwebtoken');
let multiparty = require('multiparty');
let { userModel, houseModel, tenantModel } = require('../model/model');
// 添加房源
router.post('/addhouse', async (req, res) => {
  let data = req.body;
  houseModel.create(data);
  res.status(200).send({
    msg: '添加成功',
  });
});
// 展示房源
router.get('/showhouse', async (req, res) => {
  let { houseName, houseType } = req.query;

  let data = await houseModel.find();
  if (houseName) {
    data = data.filter((item) => item.houseName.includes(houseName));
  }
  if (houseType) {
    data = data.filter((item) => item.houseType === houseType);
  }
  res.status(200).send({
    message: '查询成功',
    data,
  });
});

//展示租客
router.get('/showtenant', async (req, res) => {
  let data = await tenantModel.find().populate('tenantUser');
  res.status(200).send({
    message: '查询成功',

    data,
  });
});

//添加租客
router.post('/addtenant', async (req, res) => {
  let data = req.body;
  await tenantModel.create(data);
  res.status(200).send({
    msg: '添加成功',
  });
});

module.exports = router;
